/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:itsm-gateway
 * Create Time: 2016年1月15日 下午3:02:56
 */
package com.lenovo.lmrp.server.itsmgateway.adapter;

import java.io.InputStream;
import java.util.List;

import com.lenovo.lmrp.api.model.entity.AttachmentPO;
import com.lenovo.lmrp.api.model.entity.EngineerPO;
import com.lenovo.lmrp.api.model.entity.TicketPO;
import com.lenovo.lmrp.api.model.view.ChangedTicketVO;
import com.lenovo.lmrp.api.model.view.EngineerVO;
import com.lenovo.lmrp.api.model.view.PartsVO;
import com.lenovo.lmrp.api.model.view.TicketVO;
import com.lenovo.lmrp.server.itsmgateway.domain.metadata.ItsmEntityTranslator.EntityName;
import com.lenovo.sdc.framework.mq.IMQReceiver;
import com.lenovo.sdc.framework.util.AttributeBean;
import com.lenovo.sdc.framework.util.Tuple;

/**
 * 
 * @author wujin
 * @version $Id: AItsmAdapter.java, v 0.1 2016年1月18日 下午4:17:49 wujin Exp $
 */
public abstract class AItsmAdapter implements IMQReceiver {
    public static final String ADAPTER_ITSMCOMDE = "adapter.itemcode";
    public static final String ADAPTER_ADDRESS = "adapter.address";
    public static final String ADAPTER_CLASS = "adapter.class";
    
    private AttributeBean params;
    
    /**
     * create ITSM Adapter instacne with itsm connection parameter
     * @param params
     */
    public AItsmAdapter(AttributeBean params) {
        this.params = params;
    }
    
    /**
     * get adapter configuration parameters and ITSM-Gateway system parameters
     * 
     * @return
     */
    protected AttributeBean getParameters() {
        return params;
    }

    /**
     * authenticate engineer, used for app login
     * 
     * @param loginName engineer login name, registered in ITSM system
     * @param password
     * @return Pair first value means passed auth or not, second value is failed reason
     */
    public abstract Tuple.Pair<Boolean, String> authenticate(String loginName, String password);
    
    /**
     * reset enginerr password in ITSM, ITSM may check some engineer attribute to verify engineer
     * 
     * @param engineer engineer info, must provide loginname
     * @return Pair first value means passed auth or not, second value is failed reason
     */
    public abstract Tuple.Pair<Boolean, String> resetPassword(EngineerVO engineer);
    
    /**
     * get engineer basic information from ITSM system then convert to internal data structure
     * convert rule configured in DB, during adpater initialized this rule may load to cache to improve convert speed
     * 
     * @param loginName
     * @return
     */
    public abstract Tuple.Triplet<Boolean, String, EngineerPO> getEngineer(String loginName);
    
    /**
     * update engineer information, for example phone, email, status and password
     * only update non-empty property
     * 
     * @param engineer
     * @return
     */
    public abstract Tuple.Pair<Boolean, String> updateEngineer(EngineerVO engineer);
    
    /**
     * get ticket data from ITSM system and convert to internal data structure.
     * convert rule configured in DB, during adpater initialized this rule may load to cache to improve convert speed.
     * 
     * @param ticketID
     * @return
     */
    public abstract Tuple.Triplet<Boolean, String, TicketPO> getTicket(String ticketID);
    
    /**
     * update ticket information to ITSM system. 
     * Invoker should check response to process failed case, may need notify somebody to change field value.
     * 
     * @param ticket, changed fields in ticket
     * @return
     */
    public abstract Tuple.Pair<Boolean, String> updateTicket(ChangedTicketVO ticket);
    
    /**
     * submit parts data to ITSM system
     * 
     * @param ticketID
     * @param partsList
     * @return
     */
    public abstract Tuple.Pair<Boolean, String> replaceParts(String ticketID, List<PartsVO> partsList);
    
    /**
     * upload ticket attachment to ITSM system. If attachment has uploaded to ITSM will ignore this command
     * 
     * @param attachment
     * @return Pair first value means passed auth or not, second value is failed reason
     */
    public abstract Tuple.Pair<Boolean, String> uploadTicketAttachment(AttachmentPO attachment);
    
    /**
     * Create  routing inspection ticket
     * 
     * @param ticket
     * @return Pair first value means success or failed, second value is ticketid or failed reason
     */
    public abstract Tuple.Triplet<Boolean, String, TicketPO> createTicket(TicketVO ticket);
    
    /**
     * Import data into platform that come from ITSM exported data file
     * 
     * @param entity
     * @param input
     * @return
     */
    public abstract Tuple.Pair<Boolean, String> importEntityData(EntityName entity, InputStream input);
}
